﻿using Microsoft.EntityFrameworkCore;

namespace home_storage
{
    public class StorageDbContext : DbContext
    {
        public DbSet<Category> Categories { get; set; }
        public DbSet<FileMetadata> FileMetadata { get; set; }
        public DbSet<Tag> Tags { get; set; }

        public StorageDbContext(DbContextOptions<StorageDbContext> options) : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 使用预编译模型
           // modelBuilder.Model.AddEntityTypes(StorageDbContextModel.Instance);
         
            StorageDbContextModel.Configure(modelBuilder);
        }
    }



    #region 数据库模型
    //public class StorageDbContext : DbContext
    //{
    //    public DbSet<Category> Categories { get; set; }
    //    public DbSet<FileMetadata> FileMetadata { get; set; }
    //    public DbSet<Tag> Tags { get; set; }

    //    public StorageDbContext(DbContextOptions<StorageDbContext> options) : base(options)
    //    {
    //    }

    //    protected override void OnModelCreating(ModelBuilder modelBuilder)
    //    {
    //        modelBuilder.Entity<FileMetadata>()
    //            .HasMany(f => f.Tags)
    //            .WithMany(t => t.Files)
    //            .UsingEntity<Dictionary<string, object>>(
    //                "FileTags",
    //                j => j.HasOne<Tag>().WithMany().HasForeignKey("TagsId"),
    //                j => j.HasOne<FileMetadata>().WithMany().HasForeignKey("FilesId"),
    //                j => j.ToTable("FileTags"));
    //    }
    //}

    public class Category
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Path { get; set; } // 存储路径，如 "Images/2023"
        public string AllowedExtensions { get; set; } // 允许的文件扩展名，逗号分隔
    }

    public class FileMetadata
    {
        public string Id { get; set; } // 文件名（不含扩展名）
        public string OriginalName { get; set; }
        public string Path { get; set; } // 相对于Storage目录的路径
        public long Size { get; set; }
        public string ContentType { get; set; }
        public DateTime UploadedAt { get; set; }
        public string Description { get; set; }

        public int CategoryId { get; set; }
        public Category Category { get; set; }

        public ICollection<Tag> Tags { get; set; } = new List<Tag>();
    }

    public class Tag
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public ICollection<FileMetadata> Files { get; set; }
    }
    #endregion
}
